import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义设备领用表格行的类型
export interface EquipmentRow {
  id?: number | string
  equipmentName: string
  model: string
  serialNumber: string
  calibrationEquipment: string
  standardValue: string
  borrowStartTime: string
  borrowEndTime: string
  facilityId: number | string
  testValue1: string
  testValue2: string
  testValue3: string
  testValueAvg: string
  testValueWarning?: boolean
  testValueWarningMessage?: string
}

// 定义校准环境条件类型
export interface CalibrationEnv {
  temperature: string
  humidity: string
  pressure: string
}

// 新增：定义流量计校准信息表格行的类型
export interface FlowmeterCalibrationRow {
  id?: number | string // 可以考虑加上ID用于后续操作
  nameModel: string // 名称 (型号) 对应 FlowmeterVO 的 model
  flowmeterSerialNumber: string // 流量计编号 对应 FlowmeterVO 的 serialNumber
  flowRange: string // 流量范围 (L/min) 可以由 minFlow 和 maxFlow 组合
  // 可以根据需要添加 FlowmeterVO 中的其他字段
}

// 定义设备领用信息存储
export const useEquipmentStore = defineStore(
  'equipment',
  () => {
    // 表格数据
    const equipmentList = ref<EquipmentRow[]>([])

    // 校准环境条件
    const calibrationEnv = ref<CalibrationEnv>({
      temperature: '',
      humidity: '',
      pressure: ''
    })

    // 新增：流量计校准信息列表
    const flowmeterCalibrationList = ref<FlowmeterCalibrationRow[]>([])

    // 当前项目ID
    const currentProjectId = ref<string>('')

    // 保存设备领用信息 (修改：新增 flowmeterCalibrationList 参数)
    const saveEquipmentData = (
      data: EquipmentRow[],
      projectId: string,
      env: CalibrationEnv,
      flowmeterData: FlowmeterCalibrationRow[] // 新增参数
    ) => {
      equipmentList.value = data
      currentProjectId.value = projectId
      calibrationEnv.value = env
      flowmeterCalibrationList.value = flowmeterData // 保存流量计数据
    }

    // 清空设备领用信息 (修改：同时清空流量计列表)
    const clearEquipmentData = () => {
      equipmentList.value = []
      flowmeterCalibrationList.value = [] // 清空流量计列表
      currentProjectId.value = ''
      calibrationEnv.value = {
        temperature: '',
        humidity: '',
        pressure: ''
      }
    }

    // 添加设备
    const addEquipment = (equipment: EquipmentRow) => {
      equipmentList.value.push(equipment)
    }

    // 批量添加设备
    const addEquipmentBatch = (equipmentArr: EquipmentRow[]) => {
      equipmentList.value.push(...equipmentArr)
    }

    // 删除设备
    const removeEquipment = (index: number) => {
      equipmentList.value.splice(index, 1)
    }

    // 更新校准环境
    const updateCalibrationEnv = (env: CalibrationEnv) => {
      calibrationEnv.value = env
    }

    // 新增：添加流量计校准信息
    const addFlowmeterCalibration = (flowmeter: FlowmeterCalibrationRow) => {
      flowmeterCalibrationList.value.push(flowmeter)
    }

    // 新增：批量添加流量计校准信息
    const addFlowmeterCalibrationBatch = (flowmeterArr: FlowmeterCalibrationRow[]) => {
      flowmeterCalibrationList.value.push(...flowmeterArr)
    }

    // 新增：删除流量计校准信息
    const removeFlowmeterCalibration = (index: number) => {
      flowmeterCalibrationList.value.splice(index, 1)
    }

    return {
      equipmentList,
      calibrationEnv,
      flowmeterCalibrationList, // 导出流量计列表状态
      currentProjectId,
      saveEquipmentData,
      clearEquipmentData,
      addEquipment,
      addEquipmentBatch,
      removeEquipment,
      updateCalibrationEnv,
      addFlowmeterCalibration, // 导出新增流量计 action
      addFlowmeterCalibrationBatch, // 导出批量新增流量计 action
      removeFlowmeterCalibration // 导出删除流量计 action
    }
  },
  {
    persist: true // 启用 Pinia 持久化，会自动保存 ref 中的状态到 localStorage
  }
)
